<?php
namespace Zxf5115\Laravel\Advertising\Logics;

use Illuminate\Http\Request;
use Zxf5115\Laravel\Kernel\Utils\RequestUtil;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-09-17
 *
 * 广告逻辑处理类
 */
class AdvertisingLogic
{
  /**
   * 广告数据处理
   *
   * @param Request $request 用户请求参数
   * @param string $modelClass 操作模型类
   * @return void
   */
  public function handle(Request $request, string $modelClass) : void
  {
    $model = $modelClass::firstOrNew(['id' => $request->id]);

    $model->type = $request->type ?? 1;
    $model->position_id = $request->position_id;
    $model->title = $request->title;
    $model->picture = $request->picture ?? '';
    $model->params = $request->params ?? '';
    $model->sort = $request->sort ?? 0;
    $model->save();

    // 对用户关联对象数据进行组装
    $detail = RequestUtil::packRelevanceData($request->all(), ['content']);

    if(!empty($detail))
    {
      $where = [
        'advertising_id' => $model->id
      ];

      $model->detail()->updateOrCreate($where, $detail);
    }
  }
}
